package com.ag.wzp.dao;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.ag.bean.TbEvent;
import com.ag.dao.TbEventDAO;

public class WzpTbEventDAO extends TbEventDAO {
	private static final Log log = LogFactory.getLog(WzpTbEventDAO.class);
	private SessionFactory sessionFacotry;   
    @Resource  
    public void setSessionFacotry(SessionFactory sessionFacotry) {   
        this.sessionFacotry = sessionFacotry;   
    }   
    @PostConstruct  
    public void injectSessionFactory() {   
        super.setSessionFactory(sessionFacotry);
    }  
	@Override
	public TbEvent findById(String eid) {
		log.debug("getting TbEvent by id: " + eid);
		try {
			String hql = "from TbEvent e left join fetch e.tbEventLevel join fetch e.tbEventStatus "
					+ "join fetch e.tbEventType left join fetch e.tbEventSource left join fetch e.tbUserByUId "
					+ "left join fetch e.tbEventSource where e.EId = "
					+ eid;
			TbEvent instance = (TbEvent) getHibernateTemplate().find(hql).get(0);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List find(String hql) {
		log.debug("finding from hql");
		try {
			return getHibernateTemplate().find(hql);
		} catch (RuntimeException re) {
			log.error("find by hql", re);
			throw re;
		}
	}
	
	public List queryCount(final String cql,  final String eventTimeStart,  final String eventTimeEnd)
			throws Exception {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(cql);
				if(eventTimeStart!=null&&eventTimeStart.length()>0){
					java.sql.Date begin = java.sql.Date.valueOf(eventTimeStart);
					query.setDate("eventTimeStart", begin);
				}
				if(eventTimeEnd!=null&&eventTimeEnd.length()>0) {
					java.sql.Date end = java.sql.Date.valueOf(eventTimeEnd);
					query.setDate("eventTimeEnd",end);
				}
				List list = query.list();
				return list;
			}
		});
	}
	
	public List query(final String hsql, final int firstRow, final int maxRow)
			throws Exception {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(hsql);
//				if(eventTimeStart!=null&&eventTimeStart.length()>0){
//					java.sql.Date begin = java.sql.Date.valueOf(eventTimeStart);
//					query.setDate("eventTimeStart", begin);
//				}
//				if(eventTimeEnd!=null&&eventTimeEnd.length()>0) {
//					java.sql.Date end = java.sql.Date.valueOf(eventTimeEnd);
//					query.setDate("eventTimeEnd",end);
//				}
				query.setFirstResult(firstRow);
				query.setMaxResults(maxRow);
				List list = query.list();
				return list;
			}
		});
	}

}
